﻿using System;
using System.ComponentModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using TomNet.Core;
using TomNet.Security.Claims;

using TomNet.App.Core.Contracts.Identity;
using SqlSugar;
using System.Collections.Generic;
using TomNet.App.Model.DB.Identity;
using System.Data;
using System.Reflection;

namespace TomNet.App.Web.Controllers
{
    [Authorize]
    [Description("管理后台")]
    public class DefaultController : Controller
    {
        protected ILogger Logger => HttpContext.RequestServices.GetLogger(GetType());
        private readonly ITableMenusContract _tableMenusContract;
        public DefaultController(ITableMenusContract tableMenusContract)
        {
            _tableMenusContract = tableMenusContract;
        }

        [Description("后台首页")]
        public IActionResult Index()
        {
            var RoleId = HttpContext.User.Identity.GetClaimValueFirstOrDefault("RoleId");           
            var menus = _tableMenusContract.UnitOfWork.DbContext.Ado.GetDataTable("select * from Fn_Table_Menus("+ RoleId+")", new List<SugarParameter>(){
                  new SugarParameter("@UserID",RoleId) //参数
                });
            List<TableMenus> TableMenusModel = new List<TableMenus>();
            List<TableMenus> TableMenusModelOne = new List<TableMenus>();
            List<TableMenus> TableMenusModelTwo = new List<TableMenus>();
            TableMenusModel = ConvertToList(menus);
            for (int i = 0; i < TableMenusModel.Count; i++)
            {
                if (TableMenusModel[i].ParentID == 0)
                {
                    TableMenusModelOne.Add(TableMenusModel[i]);
                }
                else
                {
                    TableMenusModelTwo.Add(TableMenusModel[i]);
                }
            }
            ViewBag.TableMenusModel0 = TableMenusModelOne;
            ViewBag.TableMenusModel1 = TableMenusModelTwo;
            return View();
        }
        //DataTable转成List
        public static List<TableMenus> ConvertToList(DataTable dt)
        {
            // 定义集合   
            List<TableMenus> ts = new List<TableMenus>();

            // 获得此模型的类型  
            Type type = typeof(TableMenus);
            //定义一个临时变量  
            string tempName = string.Empty;
            //遍历DataTable中所有的数据行  
            foreach (DataRow dr in dt.Rows)
            {
                TableMenus t = new TableMenus();
                // 获得此模型的公共属性  
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //遍历该对象的所有属性  
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;//将属性名称赋值给临时变量  
                    //检查DataTable是否包含此列（列名==对象的属性名）    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter  
                        if (!pi.CanWrite) continue;//该属性不可写，直接跳出  
                        //取值  
                        object value = dr[tempName];
                        //如果非空，则赋给对象的属性  
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                //对象添加到泛型集合中  
                ts.Add(t);
            }
            return ts;
        }
        [Description("默认页")]
        public IActionResult Welcome()
        {
            return View();
        }
    }
}